package br.com.ufrj.ppgi.helloandre;

//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.io.PrintWriter;
//import java.net.Socket;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class StartTest extends Activity{

	private static int portNumber = 9110;
	private Socket sock;
	private PrintWriter writer;	
	private BufferedReader reader;
	private int i = 1;	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(br.com.ufrj.ppgi.helloandre.R.layout.starttest);

		Button bt = (Button) findViewById(br.com.ufrj.ppgi.helloandre.R.id.btstart);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				start();				
			}
		});
		
	}
	
//	private void setUpNetworking(){
//		try{
//			this.sock = new Socket("146.164.16.246", portNumber);
//			this.writer = new PrintWriter(this.sock.getOutputStream());
//			this.reader = new BufferedReader(new InputStreamReader(this.sock.getInputStream()));
//			System.out.println("Network established");
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
//	}

	private void start(){
	
		String message;
		
		LeXls lexls = new LeXls();
		lexls.run();
		
		// thread to listen to server response
//		Thread incomingThread = new Thread(new IncomingReader());
		
		
//		incomingThread.start();
//
//		this.setUpNetworking();
//		
		while ((message = lexls.createMessage(i++))!=null) {
			this.send(message);
		}
		
		
	}
	
	private void send(String message){
//		try{
//			writer.println(message);
//			writer.flush();
//			i++;
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
//		
		Log.v("xls", message);
	}
	
//	private class IncomingReader implements Runnable {
//
//		@Override
//		public void run() {
//			String message;
//			try{
//				while((message = reader.readLine()) != null){
//					Log.d("tv", message);
//				}
//			}catch(Exception ex) {
//				ex.printStackTrace();
//			}
//			
//		}
//		
//	}
}
